from django.views.generic import View
from django.http import HttpResponse
from django.utils.timezone import make_aware
from datetime import datetime


class IndexView(View):
    def get(self, request, *args, **kwargs):
        # TODO: 设置3分钟有效时间的cookie
        # response = HttpResponse('首页')
        # response.set_cookie('username', 'zhiliao', max_age=180, httponly=True)

        # TODO: 设置指定时间失效的cookie
        expires = datetime(year=2020, month=10, day=5, hour=0, minute=0, second=0)
        expires = make_aware(expires)
        response = HttpResponse('首页')
        response.set_cookie('username', 'zhiliao', expires=expires, httponly=True)
        # TODO: 设置指定path下的cookie
        response.set_cookie('user_id', 'abcaigjgj', expires=expires, path='/cms', httponly=True)
        return response


class ListView(View):
    def get(self, request, *args, **kwargs):
        username = request.COOKIES.get('username')
        user_id = request.COOKIES.get('user_id')
        print('user_id：', user_id)
        response = HttpResponse(username)
        return response


class CMSView(View):
    def get(self, request, *args, **kwargs):
        user_id = request.COOKIES.get('user_id')
        response = HttpResponse(user_id)
        return response


class DeleteView(View):
    def get(self, request, *args, **kwargs):
        response = HttpResponse('删除成功')
        response.delete_cookie('username')
        return response
